<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/4/7
 * Time: 14:12
 */
namespace app\service;
/**
 * Class Pay
 * @package app\service
 * 统一下单
 */
class Pay{
    protected $param;
    public function __construct($pay_type = 99,$order_amount = 0,$product_name = '控股王',$payerAcctNo = '',$payerName='',$payerTelephone='',$productPrice = 0,$productId = 1,$openid=''){
        $this->param = [
            'serverUrl'    => 'https://mobile.openepay.com/mobilepay/index.do',
            'inputCharset' => 1,
            'pickupUrl'    => 'http://'.$_SERVER['HTTP_HOST'].'/wechat/pay/paySuccess',
            'receiveUrl'   => 'http://'.$_SERVER['HTTP_HOST'].'/wechat/pay/notify',
            'version'      => 'v1.0',
            'language'     => 1,
            'signType'     => 1,
            'merchantId'   => '105840170321008',
            'payerAcctNo'  => '',
            'payerName'    => $payerName,
            'payerEmail'   => '',
            'payerTelephone' => $payerTelephone,
            'orderNo'      => date('Ymdhis',time()).rand(100000,999999),
            'orderAmount'  => $order_amount,
            'orderDatetime' => date('Ymdhis'),
            'orderCurrency' => '156',
            'orderExpireDatetime' => '',
            'productName'  => $product_name, //产品名称
            'productId'    => 1,   //产品ID编号
            'productPrice' => 100, //产品价格
            'productNum'   => 1,   //商品简述
            'productDesc'  => '',
            'ext1'         => $product_name.','.$productId,
            'ext2'         => $openid,
            'extTL'        => '',
            'termId'       => '',
            'payType'      => $pay_type,
            'issuerId'     => '',
            'key'          => 'WdOhsjsPsh5sx52sdf4sDedFsXExpnq2250',
        ];
    }
    /**
     * @return array
     * 返回需求数据
     */
    public function payParam(){
        return $this->param;
    }
    /**
     * @return string
     * 签名数据
     */
    public function signMsg(){
        $bufSignSrc="";
        if($this->param['inputCharset'] != "")
            $bufSignSrc = $bufSignSrc."inputCharset=".$this->param['inputCharset']."&";
        if($this->param['pickupUrl'] != "")
            $bufSignSrc = $bufSignSrc."pickupUrl=".$this->param['pickupUrl']."&";
        if($this->param['receiveUrl'] !=  "")
            $bufSignSrc = $bufSignSrc."receiveUrl=".$this->param['receiveUrl']."&";
        if($this->param['version'] != "")
            $bufSignSrc = $bufSignSrc."version=".$this->param['version']."&";
        if($this->param['language'] != "")
            $bufSignSrc=$bufSignSrc."language=".$this->param['language']."&";
        if($this->param['signType'] != "")
            $bufSignSrc=$bufSignSrc."signType=".$this->param['signType']."&";
        if($this->param['merchantId'] != "")
            $bufSignSrc=$bufSignSrc."merchantId=".$this->param['merchantId']."&";
        if($this->param['payerName'] != "")
            $bufSignSrc=$bufSignSrc."payerName=".$this->param['payerName']."&";
        if($this->param['payerEmail'] != "")
            $bufSignSrc=$bufSignSrc."payerEmail=".$this->param['payerEmail']."&";
        if($this->param['payerTelephone'] != "")
            $bufSignSrc=$bufSignSrc."payerTelephone=".$this->param['payerTelephone']."&";

        if($this->param['payerAcctNo'] != "")
            $bufSignSrc=$bufSignSrc."payerAcctNo=".$this->param['payerAcctNo']."&";
        if($this->param['orderNo'] != "")
            $bufSignSrc=$bufSignSrc."orderNo=".$this->param['orderNo']."&";
        if($this->param['orderAmount'] != "")
            $bufSignSrc=$bufSignSrc."orderAmount=".$this->param['orderAmount']."&";
        if($this->param['orderCurrency'] != "")
            $bufSignSrc=$bufSignSrc."orderCurrency=".$this->param['orderCurrency']."&";
        if($this->param['orderDatetime'] != "")
            $bufSignSrc=$bufSignSrc."orderDatetime=".$this->param['orderDatetime']."&";
        if($this->param['orderExpireDatetime'] != "")
            $bufSignSrc=$bufSignSrc."orderExpireDatetime=".$this->param['orderExpireDatetime']."&";
        if($this->param['productName'] != "")
            $bufSignSrc=$bufSignSrc."productName=".$this->param['productName']."&";
        if($this->param['productPrice'] != "")
            $bufSignSrc=$bufSignSrc."productPrice=".$this->param['productPrice']."&";
        if($this->param['productNum'] != "")
            $bufSignSrc=$bufSignSrc."productNum=".$this->param['productNum']."&";
        if($this->param['productId'] != "")
            $bufSignSrc=$bufSignSrc."productId=".$this->param['productId']."&";
        if($this->param['productDesc'] != "")
            $bufSignSrc=$bufSignSrc."productDesc=".$this->param['productDesc']."&";
        if($this->param['ext1'] != "")
            $bufSignSrc=$bufSignSrc."ext1=".$this->param['ext1']."&";
        if($this->param['ext2'] != "")
            $bufSignSrc=$bufSignSrc."ext2=".$this->param['ext2']."&";
        if($this->param['extTL'] != "")
            $bufSignSrc=$bufSignSrc."extTL".$this->param['extTL']."&";
        if($this->param['termId'] != "")
            $bufSignSrc=$bufSignSrc."termId=".$this->param['termId']."&";
        if($this->param['payType'] != "")
            $bufSignSrc=$bufSignSrc."payType=".$this->param['payType']."&";
        if($this->param['issuerId'] != "")
            $bufSignSrc=$bufSignSrc."issuerId=".$this->param['issuerId']."&";
        $bufSignSrc=$bufSignSrc."key=".$this->param['key']; //key为MD5密钥，密钥是在开联通支付网关商户服务网站上设置。
        //签名，设为signMsg字段值。
        $signMsg = strtoupper(md5($bufSignSrc));
        return $signMsg;
    }

}